package com.xcx.robot.api;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xcx.robot.annotation.ExceptionCatch;
import com.xcx.robot.entity.NetEaseMusic;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.message.data.MusicKind;
import net.mamoe.mirai.message.data.MusicShare;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 邢晨旭
 * @Date: 2021/7/1 13:51
 * @Description:
 */
@Service
@Slf4j
public class NetEaseMusicApi {

//    public List<NetEaseMusic> getNetEaseMusicPage(String musicName) {
//        String jsonStr = null;
//        List<NetEaseMusic> list = new ArrayList<>();
//        try {
//            // 转换成encode
//            String str = URLEncoder.encode(musicName, "utf-8");
//            //拼接url
//            URL url = new URL("http://music.163.com/api/search/pc?s=" + str + "&type=1&limit=10");
//            HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
//            httpCon.setConnectTimeout(3000);
//            httpCon.setDoInput(true);
//            httpCon.setRequestMethod("GET");
//            // 获取相应码
//            int respCode = httpCon.getResponseCode();
//            if (respCode == HttpStatus.OK.value()) {
//                // ByteArrayOutputStream相当于内存输出流
//                ByteArrayOutputStream out = new ByteArrayOutputStream();
//                byte[] buffer = new byte[1024];
//                int len = 0;
//                // 将输入流转移到内存输出流中
//                while ((len = httpCon.getInputStream().read(buffer, 0, buffer.length)) != -1) {
//                    out.write(buffer, 0, len);
//                }
//                // 将内存流转换为字符串
//                jsonStr = out.toString();
//                JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(JSONObject.parseObject(jsonStr).getString("result")).getString("songs"));
//                //jsonArray 内就是歌曲信息了 之后可以用jsonArray.getJSONObject(0).getString(/*这里放你要获取的字段名如id*/)获取详细信息
//                if (jsonArray == null) {
//                    log.info("音乐信息json为空");
//                    return null;
//                }
//                for (int i = 0; i < jsonArray.size(); i++) {
//                    NetEaseMusic netEaseMusic = new NetEaseMusic();
//                    netEaseMusic.setId(jsonArray.getJSONObject(i).getString("id"));
//                    netEaseMusic.setName(jsonArray.getJSONObject(i).getString("name"));
//                    JSONArray artists = JSONArray.parseArray(JSONObject.parseObject(jsonArray.getString(i)).getString("artists"));
//                    List<String> artistList = new ArrayList<>();
//                    for (int j = 0; j < artists.size(); j++) {
//                        artistList.add(JSONObject.parseObject(artists.getString(j)).getString("name"));
//                    }
//                    netEaseMusic.setArtists(artistList);
//                    netEaseMusic.setPicUrl(JSONObject.parseObject(jsonArray.getJSONObject(i).getString("album")).getString("picUrl"));
//                    list.add(netEaseMusic);
//                }
//                return list;
//            } else {
//                log.info("云音乐解析出错");
//                return null;
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }

    public MusicShare getMusicCard(NetEaseMusic netEaseMusic) {
        String musicUrl = "http://music.163.com/song/media/outer/url?id=" + netEaseMusic.getId() + ".mp3";
        String picUrl = netEaseMusic.getPicUrl();
        String jumpUrl = "https://music.163.com/#/song?id=" + netEaseMusic.getId();
        String title = netEaseMusic.getName();
        String summary;
        List<String> artists = netEaseMusic.getArtists();
        summary = String.join("/", artists);
        return new MusicShare(MusicKind.NeteaseCloudMusic, title, summary, jumpUrl, picUrl, musicUrl);
    }

    public String getMusicUrl(NetEaseMusic netEaseMusic) {
        return "http://music.163.com/song/media/outer/url?id=" + netEaseMusic.getId() + ".mp3";
    }

    public MusicShare getMusicCard(String musicName) {
        List<NetEaseMusic> netEaseMusic = getNetEaseMusic(musicName);
        if (CollectionUtil.isEmpty(netEaseMusic)) {
            return null;
        }
        return getMusicCard(netEaseMusic.get(0));
    }

    @ExceptionCatch
    public List<NetEaseMusic> getNetEaseMusic(String musicName) {
        String jsonStr = null;
        List<NetEaseMusic> list = new ArrayList<>();
        jsonStr = HttpUtil.get("http://music.163.com/api/search/pc?s=" + musicName + "&type=1&limit=10");
        String code = JSONObject.parseObject(jsonStr).getString("code");
        if(!"200".equals(code)){
            log.error("查询异常");
            return null;
        }
        JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(JSONObject.parseObject(jsonStr).getString("result")).getString("songs"));
        if (jsonArray == null) {
            log.error("音乐信息json为空");
            return null;
        }
        for (int i = 0; i < jsonArray.size(); i++) {
            NetEaseMusic netEaseMusic = new NetEaseMusic();
            netEaseMusic.setId(jsonArray.getJSONObject(i).getString("id"));
            netEaseMusic.setName(jsonArray.getJSONObject(i).getString("name"));
            JSONArray artists = JSONArray.parseArray(JSONObject.parseObject(jsonArray.getString(i)).getString("artists"));
            List<String> artistList = new ArrayList<>();
            for (int j = 0; j < artists.size(); j++) {
                artistList.add(JSONObject.parseObject(artists.getString(j)).getString("name"));
            }
            netEaseMusic.setArtists(artistList);
            netEaseMusic.setPicUrl(JSONObject.parseObject(jsonArray.getJSONObject(i).getString("album")).getString("picUrl"));
            list.add(netEaseMusic);
        }
        if (CollectionUtil.isEmpty(list)) {
            log.error("云音乐解析结果为空{}", jsonArray);
            return null;
        }
        return list;
    }

    public static void main(String[] args) {
        new NetEaseMusicApi().getNetEaseMusic("haha").forEach(System.out::println);
    }
}
